import { createRouter, createWebHashHistory  } from 'vue-router';
import { useAuthStore } from '@/stores/authStore';
import Home from '../views/Home.vue';
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';
import Profile from '../views/Profile.vue';
import Admin from '../views/Admin.vue';

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },

    {
        path: '/admin',
        name: 'Admin',
        component: Admin,
        meta: { requiresAdmin: true } // 仅管理员可访问
    },
    {
      path: '/profile/',
      name: 'Profile',
      component: Profile,
      props: true,
      meta: { requiresAuth: true}
    }
    // 可以根据需要添加更多路由
];

const router = createRouter({
  history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore();
  const userIsAuthenticated = authStore.isLoggedIn;
  const userIsAdmin = authStore.user?.role === 1;

  if (to.matched.some(record => record.meta.requiresAuth) && !userIsAuthenticated) {
      next({ name: 'Login' });
  } else if (to.matched.some(record => record.meta.requiresAdmin) && !userIsAdmin) {
      next({ name: 'Home' });
  } else {
      next();
  }
});

export default router;
